package com.hyperfix.common.holder;

import com.hyperfix.common.exception.CustomException;
import com.hyperfix.models.user.dtos.UserDTO;

/**
 * @author JiangChunYu
 * 2024/4/5
 */
public class UserHolder {
    private static final ThreadLocal<UserDTO> tl = new ThreadLocal<>();

    public static void saveUser(UserDTO user) {
        tl.set(user);
    }

    public static UserDTO getUser() {
        UserDTO userDTO = tl.get();
        if (userDTO == null) {
            throw new CustomException("系统出错，上下文中没有用户");
        }
        return userDTO;
    }

    public static void removeUser() {
        tl.remove();
    }
}
